/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.transform_t2d.var;

import java.util.Properties;
import org.opu.vcforms.template.bean.Var;

/**
 *  Processed variable with type - 'settings'. Settings var is
 * special variable, that contains the attributes of application
 * (settings of the current session handling). To access the
 * attribute, use the symbol "$". For example: <pre>
 *  we have variable grup = AC-073;
 *  the input string is: "student of $grup grup"
 *  in that case, the output string will be: "student of AC-073 grup"
 * </pre>
 * @author yura
 */
public class SettingsVar {

    private Properties properties;

    public SettingsVar() {
        this(null);
    }

    /**
     *  Create new instance
     * @param properties all available variable
     */
    public SettingsVar(Properties properties) {
        if (properties == null){
            properties = new Properties();
        }
        this.properties = properties;
    }

    public void setProperties(Properties properties){
        this.properties.putAll(properties);
    }

    public String data(String data) {
        return data(data, properties);
    }

    /**
     *  Parse data. if next word start with {@link Var#REGULAT}, it`s
     * replased by value from properties.
     * @param string inpt string
     * @param properties where from geting values.
     * @return output, parsed, string
     */
    public String data(String string, Properties properties){
        char[] arr = string.toCharArray();
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {

            if (Var.REGULAT.equals(String.valueOf(arr[i]))
                    && ((i + 1) < arr.length)){

                i++;
                StringBuilder lsb = new StringBuilder();
                int ch;
                boolean unlegal = true;

                for (;i < arr.length; i++){
                    ch = arr[i];
                    if (Character.isJavaIdentifierPart(ch)) {
                        lsb.append((char) ch);
                    } else {
                        i--;
                        sb.append(properties.getProperty(lsb.toString()));
                        unlegal = false;
                        break;
                    }
                }

                if (unlegal){
                    sb.append(properties.getProperty(lsb.toString()));
                }

            } else {
                sb.append(arr[i]);
            }
        }

        return sb.toString();
    }

}
